<template>
  <div>
    <!-- 左侧菜单 -->
    <div class="detail_box">
      <!-- 菜单 -->
      <div class="tab_menu">
        <div>
          <span v-for="(item,index) in menus" :class="{'choose':index===tabIndex}" @click="chooseTab(index)">{{item}}订单</span>
        </div>
      </div>
      <!-- 操作 -->
      <div class="tab_box">
        <el-radio-group @change="changeTab" v-model="tab">
          <el-radio-button :label="1">我发起</el-radio-button>
          <el-radio-button :label="2">我接收</el-radio-button>
        </el-radio-group>
      </div>

      <div class="operate_box">
        <div class="item_box">
        <div class="operate_item" v-if="tab===1">
          <label>发起申请仓库：</label>
          <el-select
            filterable
            placeholder="请选择仓库"
            v-model="sourceWarehouseId">
            <el-option
              :key="index"
              :label="item.name"
              :value="item.id"
              v-for="(item,index) in warehouseOption">
            </el-option>
          </el-select>
        </div>
        <div class="operate_item" v-if="tab===2">
          <label>接收换货申请仓库：</label>
          <el-select
            filterable
            placeholder="请选择仓库"
            v-model="warehouseId">
            <el-option
              :key="index"
              :label="item.name"
              :value="item.id"
              v-for="(item,index) in warehouseOption">
            </el-option>
          </el-select>
        </div>

        <div class="operate_item">
          <label>换货单号：</label>
          <el-input clearable placeholder="请输入换货单号" type="text" v-model="keyword"></el-input>
        </div>
          <div class="operate_item">
            <label>时间：</label>
            <el-date-picker
              v-model="proDate"
              type="datetimerange"
              align="right"
              clearable
              value-format="yyyy-MM-dd HH:mm:ss"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['00:00:00', '23:59:59']">
            </el-date-picker>
          </div>
        <div class="operate_item">
          <span @click="getTableInfo(1)" class="operate_btn search_btn">查询</span>
          <span @click="resetTable" class="operate_btn reset_btn">重置</span>
        </div>

        </div>
        <!--        <div class="btn_box" v-if="btn&&btn.add">-->
        <!--          <span @click="openPopup('')" class="operate_btn search_btn" v-if="btn.add&&tab===1">新增</span>-->
        <!--        </div>-->
      </div>
      <!-- 表格 -->
      <div class="flex_box">
        <div class="detail_part" :class="{'left_box':isSee}">
          <div class="table_box">
            <el-table
              :data="tableInfo"
              :header-cell-style="tableHeader"
              border
              ref="multipleTable"
              style="width: 100%;"
              tooltip-effect="dark"
              v-loading="loading" @sort-change="getChange">
              <el-table-column
                label="ID"
                prop="id"
                width="80">
              </el-table-column>
              <el-table-column
                label="创建时间"
                prop="1"
                sortable="custom"
                width="160">
                <template slot-scope="scope">
                  <p style="text-align: left">{{ formatTime(scope.row.createTime) }}</p>
                </template>
              </el-table-column>
              <el-table-column
                label="单号"
                width="230"
                prop="barterNo">
              </el-table-column>
              <el-table-column
                label="发起仓库名称">
                <template slot-scope="scope">
                  <span>{{ scope.row.sourceWarehouseName ? scope.row.sourceWarehouseName : '——' }}</span>
                </template>
              </el-table-column>
              <el-table-column
                label="接收仓库名称">
                <template slot-scope="scope">
                  <span>{{ scope.row.warehouseName ? scope.row.warehouseName : '——' }}</span>
                </template>
              </el-table-column>
              <el-table-column
                label="商品总数"
                prop="3"
                sortable="custom">
                <template slot-scope="scope">
                  <span>{{ scope.row.numbers ? scope.row.numbers : '0' }}</span>
                </template>
              </el-table-column>
              <el-table-column
                label="配送员名称"
                prop="4"
                sortable="custom">
                <template slot-scope="scope">
                  <span>{{ scope.row.senderName ? scope.row.senderName : '——' }}</span>
                </template>
              </el-table-column>
              <el-table-column
                label="配送员电话">
                <template slot-scope="scope">
                  <span>{{ scope.row.senderPhone ? scope.row.senderPhone : '——' }}</span>
                </template>
              </el-table-column>
              <el-table-column
                label="状态"
                width="100"
                prop="2"
                sortable="custom">
                <template slot-scope="scope">
                    <span :class="{'await':scope.row.enable===2}"
                          class="normal">{{ scope.row.enable===1 ? '已出货' : '待确认' }}</span>
                </template>
              </el-table-column>
              <el-table-column
                fixed="right"
                label="操作"
                v-if="btn&&(btn.update||btn.delete || btn.confirm || btn.queryShipment || btn.detail)"
                width="200">
                <template slot-scope="scope">
                  <div class="table_btn">
                    <span @click="function(){isSee=true;orderNo=scope.row.barterNo;itemId=scope.row.id}" class="change_info" v-if="btn.detail">详情</span>
                    <span @click="openPopup(scope.row.id)" class="change_info"
                          v-if="btn.update&&scope.row.enable===2&&tab!==2">编辑</span>
                    <span @click="goShipment(scope.row)" class="change_info" v-if="btn.queryShipment">出货单</span>
                    <span @click="openPopup(scope.row.id,isDefine=true)" class="change_info"
                          v-if="btn.confirm&&scope.row.enable===2&&tab===2">确认</span>
                    <span @click="deleteItem(scope.row)" class="change_info delete_btn"
                          v-if="btn.delete&&scope.row.enable===2&&tab!==2">删除</span>
                  </div>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <div class="record_page">
            <span class="page_count">共<i>{{ totals }}</i>条</span>
            <el-pagination
              :current-page="currentPage"
              :page-size="pageSize"
              :page-sizes="[15, 20, 30, 40]"
              :total="totals"
              @current-change="getTableInfo"
              @size-change="sizeChange"
              background
              layout="prev, pager, next, sizes,jumper">
            </el-pagination>
          </div>
        </div>
        <div class="right_box" v-if="isSee">
          <p class="page_title">{{orderNo}}订单详情 <i class="detail_close" @click="closeDetail"></i> </p>
          <detail :id="itemId"></detail>
          <p class="page_title">{{orderNo}}订单日志 <i class="detail_close" @click="closeDetail"></i> </p>
          <log :orderId="orderNo"></log>
        </div>
      </div>

    </div>
    <edit :isDefine="isDefine" :mid="menuId" :tab="tab" @listenToChild="listenToEdit" v-if="isPop"></edit>
  </div>
</template>

<script>
  import edit from "./edit";
  import log from './log/index'
  import detail from './detail/index'
  import commons from "@/utils/common";
  import {getBtn} from "@/utils/btnAuth";
  import {delBarterOrder, getBarterOrder} from "@/api/basic/warehouse/barterOrder";
  import {getOtherWarehouse} from "@/api/basic/warehouse";
  import {judgeRole} from "@/utils/warehouseAuth";
  import {formatTree} from "@/utils/menuAuth";

  export default {
    name: 'shipment',
    components: {
      edit,
      log,
      detail
    },
    data() {
      return {
        keyword: '',
        proDate:'',
        sourceWarehouseId: '', // 发起申请仓库
        warehouseId: '', // 接收申请仓库
        currentPage: 1,
        pageSize: 15,
        totals: 0,
        tab: 1,
        loading: false,
        tableInfo: [],
        warehouseOption: [],
        houseOption:[],
        senderOption: [],
        menuId: '',
        isPop: false,
        isDefine: '',
        btn: {},
        roleHouse: {},
        isSee:false,
        itemId:'',
        orderNo:'',
        sortNumber:'',
        menus:['待换货','已完成'],
        tabIndex:0,
        status:2
      }
    },
    mounted() {
      this.roleHouse = judgeRole();
      this.warehouseOption = this.roleHouse.list;
      this.sourceWarehouseId = this.roleHouse.id;
      this.getWarehouse();
      if(this.$route.query.deliveryNo) {
        this.keyword = this.$route.query.deliveryNo;
      }
      this.btn = getBtn();
      if (this.btn.query) {
        this.getTableInfo(1);
      }
    },

    methods: {
      changeTab(e){
        this.isSee = false;
        this.warehouseOption = this.roleHouse.list;
        if (e===1){
          this.sourceWarehouseId = this.roleHouse.id;
        }
        else{
          this.warehouseId = this.roleHouse.id;
        }
        this.getTableInfo(1);
      },

      chooseTab(index){
        this.isSee = false;
        this.status= index === 0 ?  2 : 1;
        this.tabIndex = index;
        this.getTableInfo(1);
      },

      //去换货单
      goShipment(obj) {
        let that = this;
        let url = '/warehouse/shipment';
        let parent = '仓库管理';
        that.$router.push({
          path: url,
          query: {
            key:formatTree(parent,url),
            recycleNo:obj.deliveryNo,
            username:obj.deliveryNo,
          }
        })
      },
      //关闭详情和日志
      closeDetail() {
        this.isSee = false;
      },
      //监听列表排序
      getChange(val) {
        let sort = parseInt(val.prop)
        if(this.sortNumber < 0) {
          this.sortNumber = sort;
        }else {
          this.sortNumber = -sort;
        }
        //重新获取列表
        this.getTableInfo(1);
      },

      getTableInfo(page) {
        let that = this;
        that.loading = true;
        that.currentPage = page;
        let queryParams = {
          keyword: that.keyword,
          sourceWarehouseId: that.tab === 2 ? '' : that.sourceWarehouseId,
          warehouseId: that.tab === 1 ? '' : that.warehouseId,
          limit: that.pageSize,
          page: page,
          sort:that.sortNumber,
          enable:that.status,
          startDate:that.proDate?that.proDate[0]:'',
          endDate:that.proDate?that.proDate[1]:'',
        };
        getBarterOrder(queryParams).then(res => {
          that.tableInfo = res.list ? res.list : [];
          that.totals = res.count;
          that.loading = false;
        }).catch((e) => {
          that.tableInfo = [];
          that.totals = 0;
          that.loading = false;
        });
      },

      resetTable() {
        this.keyword = '';
        this.pageSize = 15;
        this.sortNumber = '';
        this.proDate = '';
        this.warehouseId = this.roleHouse.id ? this.roleHouse.id : '';
        this.sourceWarehouseId = this.roleHouse.id ? this.roleHouse.id : '';
        this.getTableInfo(1);
      },

      //排除自己以外的仓库
      getWarehouse() {
        let that = this;
        getOtherWarehouse({warehouseId:this.roleHouse.id}).then(res => {
          that.houseOption = res ? res : [];
        }).catch((e) => {
          that.houseOption = [];
        })
      },

      // 移除
      deleteItem(obj) {
        let that = this;
        that.$confirm('确定删除「' + obj.barterNo + '」订单信息?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delBarterOrder(obj.barterNo).then(res => {
            that.getTableInfo(that.currentPage);
            that.$message.success({
              message:'删除成功!',
              duration:1500
              }
            );
          }).catch((e) => {
            that.$message.error({
              message:'删除失败，请重试!',
              duration:1500
            });
          });
        }).catch(() => {
        });
      },

      // 打开弹窗
      openPopup(id) {
        this.isPop = true;
        this.menuId = id;
      },

      listenToEdit(e) {
        this.isPop = false;
        this.isSee=false;
        if (e.refresh) {
          this.getTableInfo(this.currentPage)
        }
      },

      formatTime(time) {
        return commons.formatTime(time)
      },

      sizeChange(val) {
        this.pageSize = val;
        this.getTableInfo(1)
      },

      // 设置table标题居中
      tableHeader({row, column, rowIndex, columnIndex}) {
        return 'background:#F0F2F5;color:#111;padding:10px 0;font-size:14px;font-weight:600;'
      },
    }
  }
</script>

<style scoped>
  .flex_box {
    display: flex;
  }
  .detail_part {
    width: 100%;
  }
  .left_box{
    width: 60%;
  }
  .right_box{
    width: 40%;
  }
  .page_title {
    font-weight: 600;
    padding-left: 20px;
    margin-bottom: 10px;
    font-size: 18px;
    position: relative;
  }
  .tab_box {
    margin-bottom: 10px;
  }
</style>
